'''
Utilities for timestamp
'''
import datetime
import os


def get_now():
    '''Get current datetime.datetime'''
    return datetime.datetime.now()


def get_date(now):
    '''Get current date with format "%Y_%m_%d"'''
    return now.strftime('%Y_%m_%d')


def get_timestamp(now):
    '''Get current timestamp in milliseconds'''
    return int(now.timestamp() * 1000)


class Timestamp():
    '''
    Wrapped timestamp.
    '''

    def __init__(self):
        self._last = get_timestamp(get_now())

    def refresh(self, timestamp):
        '''Store new timestamp'''
        self._last = timestamp

    @property
    def timestamp(self):
        '''Get stored timestamp'''
        return self._last


class PersistTimestamp(Timestamp):
    '''
    Wrapped timestamp.

    The value will be stored in a file.
    '''

    def __init__(self, path='.lastrun'):
        super().__init__()
        self._path = path
        if os.path.isfile(path):
            with open(path) as obj:
                self._last = int(obj.readline())

    def refresh(self, timestamp):
        '''Store new timestamp, and persist in file'''
        self._last = timestamp
        with open(self._path, 'w') as obj:
            obj.write(str(timestamp))
